/**
 * Copyright (C) 2011 The Android Open Source Project 
 * Emial : guoyil199@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package com.example.app.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ContextThemeWrapper;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.example.app.R;

/**
 * @author Administrator
 * @Email guoyil199@163.com
 * @version V1.0
 * @Date 2015年7月8日 下午3:07:44
 * @Title <pre>
 * todo
 * </pre>
 * @Description <pre>
 * 
 *     <RelativeLayout android:id="@+id/top"
 *     	android:layout_width="fill_parent"
 *     	android:layout_alignParentTop="true"
 *     	android:paddingLeft="10dp"
 *     	android:paddingRight="10dp"
 *     	android:background="@drawable/top_background"
 *     	android:layout_height="wrap_content">
 *     	
 *         <Button android:id="@+id/searchView_btnsearch"
 *         	android:layout_width="wrap_content"
 *         	android:layout_height="wrap_content"
 *         	android:gravity="center"
 *         	android:layout_centerVertical="true"
 *         	android:layout_alignParentRight="true"
 *         	android:textSize="12sp"
 *         	android:textStyle="bold"
 *         	android:background="@drawable/search_btn_background"
 *         	android:text="搜索"/>
 *         
 *         <RelativeLayout android:id="@+id/rlSearchFrameDelete"
 *             android:layout_width="fill_parent"
 *             android:layout_height="wrap_content"
 *         	android:layout_centerVertical="true"
 * 			android:gravity="center_vertical"
 *             android:layout_toLeftOf="@id/searchView_btnsearch">
 *             
 * 		    	<EditText android:id="@+id/searchView_edittext"
 * 		        	android:layout_width="fill_parent"
 * 		        	android:layout_height="wrap_content"
 * 					android:singleLine="true"
 * 		        	android:background="@drawable/search_frame"
 * 		        	android:layout_marginRight="10dp"
 * 		        	android:paddingLeft="32dp"
 * 		        	android:textSize="12sp"
 * 		        	android:hint="请输入文字..."/>
 * 		    	
 * 		    	<ImageView android:id="@+id/searchView_imgbtndel"
 * 		    	    android:layout_width="wrap_content"
 * 		    	    android:layout_height="wrap_content"
 * 		    	    android:layout_alignParentRight="true"
 * 		    	    android:src="@drawable/delete"
 * 		    	    android:layout_centerInParent="true"
 * 		    	    android:paddingRight="20dp"
 * 		    	    android:visibility="gone"/>
 *         </RelativeLayout>
 *     </RelativeLayout>
 * </pre>
 */

public class SearchView extends RelativeLayout {

	/**
	 * @Description <pre>
	 * Constructor
	 * </pre>
	 * @param context
	 */
	public SearchView(Context context) {
		this(context, null, R.attr.searchViewStyle);
	}

	/**
	 * @Description <pre>
	 * Constructor
	 * </pre>
	 * @param context
	 * @param attrs
	 */

	public SearchView(Context context, AttributeSet attrs) {
		this(context, attrs, R.attr.searchViewStyle);
	}

	private EditText editText;
	private Button btnSearch;
	private ImageButton imgDel;
	private RelativeLayout searchLayout;

	/**
	 * @Description <pre>
	 * Constructor
	 * </pre>
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */

	public SearchView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		
		btnSearch = new Button(new ContextThemeWrapper(context, R.style.searchViewBtnStyle),null,R.style.searchViewBtnStyle);
		btnSearch.setId(R.id.searchView_btnsearch);
		LayoutParams btnParam = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		btnParam.addRule(CENTER_VERTICAL);
		btnParam.addRule(ALIGN_PARENT_RIGHT);
		addView(btnSearch, btnParam);
		
		searchLayout = new RelativeLayout(new ContextThemeWrapper(context,R.style.searchViewLayoutStyle),attrs,R.style.searchViewLayoutStyle);
		searchLayout.setId(R.id.searchview_layoutId);
		editText = new EditText(new ContextThemeWrapper(context, R.style.searchViewEditStyle),attrs,R.style.searchViewEditStyle);
		editText.setSingleLine();
		editText.setId(R.id.searchView_edittext);
		LayoutParams etparam = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
		etparam.rightMargin = 15;
		etparam.addRule(ALIGN_PARENT_LEFT);
		searchLayout.addView(editText, etparam);

		imgDel = new ImageButton(context, attrs);
		imgDel.setId(R.id.searchView_imgbtndel);
		imgDel.setBackgroundDrawable(null);
		LayoutParams delparam = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		delparam.addRule(ALIGN_PARENT_RIGHT | CENTER_IN_PARENT);
		searchLayout.addView(imgDel, delparam);

		LayoutParams sLParam = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
		sLParam.addRule(CENTER_VERTICAL);
		sLParam.addRule(LEFT_OF, R.id.searchView_btnsearch);
		addView(searchLayout, sLParam);

		
	}

}
